package com.ruoyi.system.controller;

import java.util.List;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.system.dto.ReadRankDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.system.domain.ReadRank;
import com.ruoyi.system.service.IReadRankService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.core.utils.ip.IpUtils; // 引入 IpUtils 工具类 // 引入 ServletUtils 工具类

/**
 * 阅读排行Controller
 * 
 * @author saruri
 * @date 2024-09-12
 */
@RestController
@RequestMapping("/rank")
public class ReadRankController extends BaseController
{
    @Autowired
    private IReadRankService readRankService;


    @GetMapping("/topUrls")
    public TableDataInfo  getTopUrls(@RequestParam(required = false) String readCategory) {
        startPage();
        List<ReadRankDTO> topUrls = readRankService.getTopUrls( readCategory);
        return getDataTable(topUrls);
    }

    /**
     * 查询阅读排行列表
     */

    @GetMapping("/list")
    public TableDataInfo list(ReadRank readRank)
    {
        startPage();
        List<ReadRank> list = readRankService.selectReadRankList(readRank);
        return getDataTable(list);
    }

    /**
     * 导出阅读排行列表
     */
    @RequiresPermissions("system:rank:export")
    @Log(title = "阅读排行", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ReadRank readRank)
    {
        List<ReadRank> list = readRankService.selectReadRankList(readRank);
        ExcelUtil<ReadRank> util = new ExcelUtil<ReadRank>(ReadRank.class);
        util.exportExcel(response, list, "阅读排行数据");
    }

    /**
     * 获取阅读排行详细信息
     */
    @RequiresPermissions("system:rank:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(readRankService.selectReadRankById(id));
    }

    /**
     * 新增阅读排行
     */

    @Log(title = "阅读排行", businessType = BusinessType.INSERT)
    @PostMapping
   // public AjaxResult add(@RequestBody ReadRank readRank)
//    {
//        return toAjax(readRankService.insertReadRank(readRank));
//    }

    public AjaxResult add(@RequestBody ReadRank readRank)
    {
        // 使用 ServletUtils 获取客户端的 IP 地址
        String clientIp = IpUtils.getIpAddr();

        // 将 IP 地址存入 readRank 对象
        readRank.setIp(clientIp);

        // 保存到数据库
        return toAjax(readRankService.insertReadRank(readRank));
    }

    /**
     * 修改阅读排行
     */
    @RequiresPermissions("system:rank:edit")
    @Log(title = "阅读排行", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ReadRank readRank)
    {
        return toAjax(readRankService.updateReadRank(readRank));
    }

    /**
     * 删除阅读排行
     */
    @RequiresPermissions("system:rank:remove")
    @Log(title = "阅读排行", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(readRankService.deleteReadRankByIds(ids));
    }
//TAG_START:ReadRank_Mine_list

// 控制器
@GetMapping("/mine")
public TableDataInfo listMine(ReadRank readRank) {
    startPage();
    readRank.setUserId(SecurityUtils.getUserId());
    List<ReadRank> list = readRankService.selectReadRankListMine(readRank);
    return getDataTable(list);
}

//TAG_END:ReadRank_Mine_list


}
